<?php

namespace app\admin\validate;

use think\Validate;

class Order extends Validate
{
    protected $rule = [
        'is_del' => 'in:0,1',
        'image' => 'max:255',
        'create_time' => 'datetime',
        'creator' => 'integer',
        'update_time' => 'datetime',
        'updater' => 'integer',
        'id' => 'integer|gt:0',
        'wid' => 'integer|gt:0',
        'list' => 'array',
        'contract_type'          => 'require|in:1,2', // 签署合同类型，必须是1或2
        'signatory'              => 'require|integer', // 签订人，必须是数字
        'signing_date'           => 'require|dateFormat:Y-m-d',   // 签订日期，必须是日期格式 YYYY-MM-DD
        'single_order_method'    => 'require|in:1,2,3', // 成单方式，必须是1、2或3
        'id_number'              => 'max:100|requireIf:contract_type,1', // 统一社会信用代码/身份证号，最大长度100
        'payment_deadline'       => 'require|dateFormat:Y-m-d', // 收款截止日期，必须是日期格式
        'shipping_start_date'    => 'require|dateFormat:Y-m-d', // 发货起始日期，必须是日期格式
        'shipping_end_date'      => 'dateFormat:Y-m-d', // 发货截止时间，必须是日期格式
        'transfer_payment_info'  => 'max:50', // 付款信息，最大长度50
        'order_num'              => 'integer|gt:0', // 订单商品数量，必须是正整数
        'advance_pay_time'       => 'dateFormat:Y-m-d H:i:s', // 预付款时间，必须是日期格式，可空
        'logistics_company'      => 'require|integer|gt:0',
        'order_id'               => 'require',
        'order_type'             => 'require',
        'pay_terms'              => 'require|in:0,1', // 支付条款，必须是0或1
        'advance_amount'         => 'float|requireIf:pay_terms,1', // 预付款金额，必须是浮点数，且大于0
        'product_list'           => 'require|array', // 商品明细，必须是数组
        'source'                 => 'integer|gt:0', // 订单来源：
        'order_item_id'          => 'require|integer|gt:0',
        'wholesal_id'            => 'integer|gt:0',
    ];

    // 定义验证提示信息
    protected $message = [
        'signatory.require'         => '签订人不能为空',
        'signatory.integer'         => '签订人必须是数字',
        'signing_date.require'      => '签订日期不能为空',
        'signing_date.dateFormat'   => '签订日期格式不正确，应该是 YYYY-MM-DD',
        'single_order_method.require' => '成单方式不能为空',
        'single_order_method.in'    => '成单方式无效，必须是 1、2 或 3',
        'id_number.max'             => '统一社会信用代码/身份证号不能超过100个字符',
        'id_number.requireIf'       => '国内销售合同，统一社会信用代码/身份证号不能为空',
        'payment_deadline.require'  => '收款截止日期不能为空',
        'payment_deadline.dateFormat' => '收款截止日期格式不正确，应该是 YYYY-MM-DD',
        'shipping_start_date.require' => '发货起始日期不能为空',
        'shipping_start_date.dateFormat' => '发货起始日期格式不正确，应该是 YYYY-MM-DD',
        'shipping_end_date.dateFormat' => '发货截止时间格式不正确，应该是 YYYY-MM-DD',
        'transfer_payment_info.max' => '付款信息不能超过50个字符',
        'contract_type.require'     => '签署合同类型不能为空',
        'contract_type.in'          => '签署合同类型无效，必须是 1 或 2',
        'order_num.integer'         => '订单商品数量必须是整数',
        'order_num.gt'              => '订单商品数量必须大于0',
        'advance_pay_time.dateFormat' => '预付款时间格式不正确，应该是 YYYY-MM-DD HH:mm:ss',
        'wid' => 'Wid 不能为空',
        'logistics_company.require' => '物流公司不能为空',
        'logistics_company.integer' => 'logistics_company 字段错误',
        'logistics_company.gt' => 'logistics_company 字段错误',
        'order_id' => '订单ID不能为空',
        'order_type'                     => '订单类型不能为空',
        'pay_terms.require'              => '支付条款不能为空',
        'pay_terms.in'                   => '支付条款无效，必须是 0 或 1',
        'advance_amount.float'           => '预付款金额必须是浮点数',
        'advance_amount.requireIf'       => '支付条款为预付款时，预付款金额不能为空',
        'product_list.require'           => '商品明细不能为空',
        'product_list.array'             => '商品明细必须是数组',
        'order_id.require'               => '订单ID不能为空',
        'order_id.integer'               => '订单ID必须是数字',
        'order_id.gt'                    => '订单ID必须大于0',
        'order_item_id.require'          => '订单明细ID不能为空',
        'order_item_id.integer'          => '订单明细ID必须是数字',
        'order_item_id.gt'               => '订单明细ID必须大于0',
        'wholesal_id.require'            => '经销商订单ID不能为空',
        'wholesal_id.integer'            => '经销商订单ID必须是数字',
        'wholesal_id.gt'                 => '经销商订单ID必须大于0'
    ];

    protected $scene = [
        'search' => ['order_no' => 'max:50', 'source' => 'integer'],
        'delete' => ['ids' => 'require|array|checkIds'],
        'show' => ['id' => 'require'],
        'examine' => ['id'],
        'transSave' => ['list'],
        'save' => ['signatory', 'signing_date', 'single_order_method', 'payment_deadline', 'shipping_start_date', 'shipping_end_date', 'contract_type', 'order_num', 'advance_pay_time', 'pay_terms', 'advance_amount|requireIf:pay_terms,1|checkAdvanceAmount'],
        'selectWarehouseProducts' => ['id'],
        'delivery' => ['wid', 'order_id', 'logistics_company'],
        'deliver_express' => ['signatory', 'signing_date', 'single_order_method', 'id_number', 'payment_deadline', 'shipping_start_date', 'shipping_end_date', 'contract_type', 'order_num', 'advance_pay_time', 'pay_terms', 'advance_amount|requireIf:pay_terms,1|checkAdvanceAmount'],
        'deliver_self' => ['signatory', 'signing_date', 'single_order_method', 'id_number', 'payment_deadline', 'shipping_start_date', 'shipping_end_date', 'contract_type', 'order_num', 'advance_pay_time', 'pay_terms', 'advance_amount|requireIf:pay_terms,1|checkAdvanceAmount'],
        'receipt' => ['order_id'],
        'getInventory' => ['wid'],
        'getApprovalRecords' => ['order_id', 'order_type'],
        'getOperationRecords' => ['order_id', 'order_type'],
        'cancel' => ['id'],
        'lockInventory' => ['id'],
        'contractPreview' => ['order_id'],
        'checkExceptionOrderItem' => ['id', 'product_list'],
        'noDelivery' => ['id',  'order_item_id' => 'require'],
        'inventory_trans_save_info' => ['order_id' => 'require|integer|gt:0', 'order_item_id', 'wholesal_id']
    ];

    protected function checkAdvanceAmount($value, $rule, $data)
    {
        if (isset($data['pay_terms']) && $data['pay_terms'] == 1) {
            return is_numeric($value) && $value > 0 ? true : '预付款金额必须大于0';
        }
        return true;
    }
}
